package libs.yzx.zaza;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/8/15.
 */
public class SubStringActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_sub_str);
    }


    public void dodo(View view) {
        String sourceStr = ((EditText) findViewById(R.id.et)).getText().toString();
        sourceStr = jie(sourceStr);
        ((TextView) findViewById(R.id.tv)).setText(sourceStr);

        Toast.makeText(SubStringActivity.this, ""+(getCharLen('我')), Toast.LENGTH_SHORT).show();
    }


    public static String jie(String s) {
        StringBuilder sb = new StringBuilder();
        char[] cs = s.toCharArray();
        int totalCount = 0;
        for (int i = 0; i < cs.length; i++) {
            sb.append(cs[i]);
            totalCount += getCharLen(cs[i]);
            if (totalCount >= 12)
                break;
        }
        return sb.toString();
    }

    public static int getCharLen(char c) {
        return (c + "").getBytes().length;
    }

}
